
package com.catsic.zcjc.web.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.catsic.base.controller.BaseController;
import com.catsic.zcjc.condition.EquipFileDataCondition;
import com.catsic.zcjc.entity.EquipFileData;
import com.catsic.zcjc.form.EquipFileDataForm;
import com.catsic.zcjc.service.EquipFileDataService;

@Controller
@RequestMapping("/equipFileData")
public class EquipFileDataController extends BaseController {

	@Autowired
	private EquipFileDataService equipFileDataService;

	/**
	 * 查询设备数据附件
	 * 
	 * @author xietiebing
	 * @param condition
	 * @return
	 */
	@RequestMapping(value = "/query", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> query(EquipFileDataCondition condition, Pageable pageable) {
		Page<EquipFileDataForm> page = equipFileDataService.findAll(condition, pageable);

		List<EquipFileDataForm> list = page.getContent();
		if (CollectionUtils.isNotEmpty(list)) {
			for (EquipFileDataForm form : list) {
				equipFileDataService.findFormatDrawer(form);
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put(CONTENT, page);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}

	@RequestMapping(value = "/download", method = RequestMethod.GET)
	public void showFile(@RequestParam(value = "id") String id, HttpServletRequest request,
			HttpServletResponse response) {
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Cache-Control", "no-store");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);

		EquipFileData dataFile = equipFileDataService.findById(id);
		if (dataFile != null) {
			File file = new File(dataFile.getFilePath() + dataFile.getFileName());
			response.setCharacterEncoding("utf-8");
			response.setContentType("multipart/form-data");
			response.setHeader("Content-Disposition", "attachment;fileName=\"" + dataFile.getFileName() + "\"");
			try {
				InputStream inputStream = new FileInputStream(file);
				OutputStream os = response.getOutputStream();
				byte[] b = new byte[1024];
				int length;
				while ((length = inputStream.read(b)) > 0) {
					os.write(b, 0, length);
				}
				inputStream.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			response.setHeader("content-type", "text/html;charset=UTF-8");
			try {
				String data = "该记录无附件！";
				OutputStream outputStream = response.getOutputStream();
				byte[] dataByteArr = data.getBytes("UTF-8");
				outputStream.write(dataByteArr);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
